<?php
/**
 * 《全国区县》实现类
 *
 * @author 孙晓晔
 * @version $Id: District.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/InfoUtil.class.php');

class District extends InfoUtil {

	public $tbl = Constant::tbl_district;

	public $orderby = array(
		'-' => '请选择',
		'code' => '编号',
		'province' => '省份',
		'orderby' => '排序',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$province = $this->code_option('4');
		$controller->assign_by_ref('province_option', $province);

		$controller->display($request, 'info/district/find');
		return true;
	}

	function _where($request) {
		$province_s = $request['province_s'];
		$zone_s = $request['zone_s'];

		$where = " WHERE 1=1";

		if (!empty($province_s) && $province_s <> '-') {
			$where .= " AND code = '" . $province_s . "'";
		}

		if (!empty($zone_s)) {
			$where .= " AND zone = '" . $zone_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row['category'] = $this->category[$row['category']];

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'info/district/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/district/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$province = $request['province'];

		$city = $request['city'];
		$district = $request['district'];

		$orderby = $request['orderby'];

		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(province, city, district, orderby";
		$fmt .= ", status, createtime, modifytime) VALUES('%s', '%s', '%s'";
		$fmt .= ", '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $province, $city, $district, $orderby, $status);
		// debug($sql);
		$db->query($sql);

		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/district/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$province = $request['province'];
		$district = $request['district'];
		$city = $request['city'];

		$orderby = $request['orderby'];

		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET province = '%s', city = '%s', district = '%s', orderby = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $province, $city, $district, $orderby, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/district/view');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function importPost($controller, $request) {
		$this->pretreat($controller);

		$data = $request['data'];
		// echo $data;

		$aa = preg_split("/[\\n]+/i", $data);
		// print_r($a);

		$province = '';
		$db = newdb($this->dsn);
		while (list($k, $v) = each($aa)) {
			$buf = trim($v);
			if (strlen($buf) < 5) {
				continue;
			}

			$a = preg_split("/[\t|\s]+/i", $buf);
			// print_r($a);
			if (count($a) < 2) {
				$province = $buf;
				continue;
			}

			$city = trim($a[0]);
			$zone = trim($a[1]);

			$fmt = "INSERT INTO %s(`province`, city, zone) VALUES('%s', '%s', '%s')";
			$sql = sprintf($fmt, $this->tbl, $province, $city, $zone);
			// debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_zjm($controller, $request) {
		
		$data = $this->_ajax_zjm($request);
		echo json_encode($data);
	}

	function _ajax_zjm($request) {
		$province = $request['province'];
		$city = $request['city'];
		
		$q = $request['q'];
		
		$md5 = md5($proinvce . "_" . $city . "_" . $q);
		$key = sprintf("%s_%s_%s", $this->zjm, $this->prefix, $md5);
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($this->dsn);
		if (empty($q)) {
			$fmt = "select id, province, city, district, zjm from %s where province = '%s' and city = '%s'";
			$sql = sprintf($fmt, Constant::tbl_district, $province, $city);
		} else {
			$fmt = "select id, province, city, district, zjm from %s where province = '%s' and city = '%s' and zjm like '%s'";
			$sql = sprintf($fmt, Constant::tbl_district, $province, $city, $q . "%");
		}
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			// $row['dept_t'] = Name::dept($row['dept']);

			$ret[] = $row;
		}
		
		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("3 day"));
		}

		return $ret;
	}

	function ajax_region($controller, $request) {
		$province = $request['province'];
		$city = $request['city'];
		$region = $request['region'];
		$identity = $request['identity'];

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(province, city, region, identity) VALUES('%s', '%s', '%s', '%s')";
		$sql = sprintf($fmt, 'fy.region', $province, $city, $region, $identity);
		// debug($sql);
		$db->query($sql);
	}

	function ajax_debug($controller, $request) {
		debug($request['s']);
	}

	function iso7064($vString) {
		// ISO 7064:1983.MOD 11-2
		// by goseaside@sina.com
		$wi = array(1, 2, 4, 8, 5, 10, 9, 7, 3, 6);
		$hash_map = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');

		$i_size = strlen($vString);
		$bModify = '?' == substr($vString, -1);

		$i_size1 = $bModify ? $i_size : $i_size + 1;
		for ($i = 1; $i <= $i_size; $i++) {
			$i1 = $vString[$i - 1] * 1;
			$w1 = $wi[($i_size1 - $i) % 10];
			$sigma += ($i1 * $w1) % 11;
		}
		
		if ($bModify) { 
			return str_replace('?', $hash_map[($sigma % 11)], $vString);
		} else { 
			return $hash_map[($sigma % 11)];
		}
	}

	function b($controller, $request) {
		$s = "36012219771123391";
		$s = "360122197711233917";
		$dd = substr($s, 6, 8);
		// echo fmod(3, 2);

		// echo $dd;
		
		echo date("Y-m-d", strtotime($dd));
		
		// echo $this->iso7064($s); 
		// echo $this->iso7064("$s?"); 
	}

	function emp($controller, $request) {
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, Constant::tbl_emp);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$idcard = $row['idcard'];
			$identity = substr($idcard, 0, 6);
			echo $row['name'] . ", " . $identity . "\n";
				
			if ($r = $this->_region($identity)) {
				$fmt = "UPDATE %s SET province = '%s', city = '%s', district = '%s' WHERE id = '%s'";
				$sql = sprintf($fmt, Constant::tbl_emp, $r['province'], $r['city'], $r['region'], $row['id']);
				echo $sql . "\n";
				$db->query($sql);
			}
				
		}
	}

	function _region($identity) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE identity = '%s'";
		$sql = sprintf($fmt, "fy.region", $identity);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			return $db->fetch_array($result);
		}

		return false;
	}

	function z($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$zjm = getPinyin($row['district']);
			$zjm = strtoupper($zjm);

			$fmt = "UPDATE %s SET zjm = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $zjm, $row['id']);
			// debug($sql);
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
}
?>